package com.xiaoai.httpClientTestService.httpclient.utils;

import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;

import java.util.concurrent.TimeUnit;

public class HttpUtils {
    static PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();

    static RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(30, TimeUnit.SECONDS)
            .setConnectionRequestTimeout(10,TimeUnit.SECONDS)
            .setResponseTimeout(10,TimeUnit.SECONDS)
            .build();

    static {
        //最大连接数
        manager.setMaxTotal(300);
        //每个路由最大连接数，路由是根据host来管理的，所以这里的数量不太容易把握。
        manager.setDefaultMaxPerRoute(20);

    }

    //获取连接对象
    public static CloseableHttpClient getHttpConnection(){
        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(manager)
                .setDefaultRequestConfig(requestConfig)
                .build();
        return httpClient;
    }

}
